var dbFile = air.File.applicationDirectory.resolvePath('fileDetails.db'); 
var remoteAJAXHandlerURL = 'http://www.childdisabilityconnection.org/admin/ajaxDirector.php';

var appLocalDataObj = {
	dataConn : function() {
		conn = new air.SQLConnection(); 
		conn.open(dbFile, air.SQLMode.UPDATE); 
	}
}

var appGetDataObj = {
	getCategories : function(siteArea,mainCatID) {
		if (mainCatID>0) {
			$('fileSubCat').options.length = 0;
			var params = '?handler=getCategories&siteArea=null&renderType=json&catType=sub&catID='+mainCatID;
			var renderTo = $('fileSubCat');			
		} else {
			var params = '?handler=getCategories&siteArea='+siteArea+'&renderType=json';	
			var renderTo = $('fileCat');			
		}				
		var ajaxRequestURL = remoteAJAXHandlerURL+params;
		var ajaxResults = new Ajax.Request(ajaxRequestURL, {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {	
				if (transport.responseText != 0) {
					if (mainCatID>0) {
						$('subCatContainer').style.display = '';
					}
					var stringJSON = transport.responseText;
					var jsonObject = stringJSON.evalJSON();
					var totalItems = (jsonObject.Categories.length)-1;
					var catType    = jsonObject.CatType;
					for(var a=0;a<=totalItems;a++) {
						var catTitle = jsonObject.Categories[a].CatTitle;
						var catID    = jsonObject.Categories[a].CatID;
						renderTo.options[a] = new Option(catTitle,catID);	
					}
				} else {
					$('subCatContainer').style.display = 'none';					
				}
			}
		});
	},
	toggleResourceCats : function() {
		if ($('ptiSharedResource').checked == true) {
			this.getCategories('Shared Resource File');
		} else {
			this.getCategories('Resource File');
		}
	},
	toggleResourceCatCheckbox : function() {
		$('fileCat').options.length = 0;
		if ($('ptiSharedResource').checked == true) {
			$('ptiSharedResource').checked = false;
		} else {
			$('ptiSharedResource').checked = true;
		}
		this.toggleResourceCats();
	},
	getFileDetails : function(fileName) {
		var sql =   "SELECT CatID,FileTitle,FileDesc,SubCatID from Files WHERE FileName = '"+fileName+"'";
		var sqlStmt = new air.SQLStatement(); 
		sqlStmt.sqlConnection = conn; 
		sqlStmt.text = sql; 
		sqlStmt.execute();	
		var result = sqlStmt.getResult();
		if (result.data != null) {
			var numResults = result.data.length; 
			if (numResults>0) {				
				var catID     = result.data[0].CatID;
				var subCatID  = result.data[0].SubCatID;
				var fileTitle = result.data[0].FileTitle;
				var fileDesc  = result.data[0].FileDesc;
				
				var dataString = {
					fileName:  fileName,
					catID:     catID,
					subCatID:  subCatID,
					fileTitle: fileTitle,
					fileDesc:  fileDesc
				}
				var jsonString = Object.toJSON(dataString);			
			}
		} else {
			var dataString = {
				fileName:  fileName,
				catID:     '',
				subCatID:  '',
				fileTitle: '',
				fileDesc:  ''
			}
			var jsonString = Object.toJSON(dataString);
		}
		/*
		 * Push file details to server
		 */
				
		var request    = new air.URLRequest(uploadDestination);
		request.method = air.URLRequestMethod.POST; 
		var variables  = new air.URLVariables(); 
		variables.jsonString  = jsonString;
		variables.fileDetails = 1;
		request.data   = variables; 
		
		var loader = new air.URLLoader(); 
		loader.dataFormat = air.URLLoaderDataFormat.TEXT; 
		loader.addEventListener(air.Event.COMPLETE, completeHandler); 
		loader.load(request); 

		function completeHandler(event) { 
		    air.trace(event.target.data);
		}				
	}
}

var appSaveDateObj = {
	saveFileDetails : function() {
		var fileCat    = $F('fileCat');
		var fileSubCat = $F('fileSubCat');
		var fileTitle  = $F('fileTitle');
		var fileDesc   = $F('fileDesc');
		var fileName   = $F('fileName');
		
		/*
		 * Delete any records in the database for this file so we don't wind
		 * up with multiple records for the same file.
		 */
		var sqlDelete = "DELETE FROM Files WHERE FileName = '"+fileName+"'";
		var deleteStmt = new air.SQLStatement(); 
		deleteStmt.sqlConnection = conn; 
		deleteStmt.text = sqlDelete; 
		deleteStmt.execute();	
		
		/*
		 * Now, insert record data
		 */
		var sql =   "INSERT INTO Files (CatID,FileTitle,FileDesc,FileName,SubCatID) " +  
    				"VALUES ("+fileCat+", '"+fileTitle+"', '"+fileDesc+"','"+fileName+"',"+fileSubCat+")";
    	var sqlStmt = new air.SQLStatement(); 
		sqlStmt.sqlConnection = conn; 
		sqlStmt.text = sql; 
		sqlStmt.execute();		
	}
}

var appDeleteDataObj = {
	clearFilesTable : function() {
		var sql =   "DELETE FROM Files"; 
    	var deleteStmt = new air.SQLStatement(); 
		deleteStmt.sqlConnection = conn; 
		deleteStmt.text = sql; 
		deleteStmt.execute();
	}
}